﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Dotnet.FormExtensions
{
    public static partial class Ext
    {
        public static void AutoSetTextWidth(this TextBox txtBox,string text)
        {
            txtBox.Text = text;
            float dStrLength = Convert.ToInt32(Math.Ceiling(txtBox.CreateGraphics().MeasureString(txtBox.Text, txtBox.Font).Width));
            if (string.IsNullOrEmpty(text) == true)
                dStrLength = 80;
            ((TableLayoutPanel)txtBox.Parent).ColumnStyles[0].Width = dStrLength + 10;
        }
        /// <summary>
        /// 设置TextBox宽度随字符串长度变化
        /// </summary>
        /// <param name="textBox">当前TextBox</param>
        /// <param name="str">字符串</param>
        /// <param name="action">回调设置长度</param>
        public static void FillString(this TextBox textBox, string str, Action<float> action)
        {
            textBox.Text = str;
            float dStrLength = Convert.ToInt32(Math.Ceiling(textBox.CreateGraphics().MeasureString(textBox.Text, textBox.Font).Width));
            if (string.IsNullOrEmpty(str) == true)
                dStrLength = 80;
            action(dStrLength + 20);
        }
        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="textbox"></param>
        /// <param name="txt"></param>
        /// <param name="isTime"></param>
        public static void AppendTextAsync(this TextBox textbox, string txt, bool isTime = true)
        {
            try
            {
                if (textbox.InvokeRequired)
                {
                    var InvokeCallback = new Action<TextBox, string, bool>(AppendTextAsync);
                    if (textbox.IsDisposed == true)
                        return;
                    textbox.Invoke(InvokeCallback, textbox, txt, isTime);
                }
                else
                {
                    if (textbox.IsDisposed)
                        return;
                    if (isTime)
                    {
                        textbox.AppendText($"【{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")}】{txt}\r\n");
                    }
                    else
                    {
                        textbox.AppendText(txt + "\r\n");
                    }
                }
            }
            catch (Exception ex)
            {

                //throw;
            }
        }
        /// <summary>
        /// 当超过总行数时则清空文本框
        /// </summary>
        /// <param name="textbox"></param>
        /// <param name="txt"></param>
        /// <param name="lines">总行数</param>
        /// <param name="isTime"></param>
        public static void AppendTextAsync(this TextBox textbox, string txt,int lines, bool isTime = true)
        {
            try
            {
                if (textbox.InvokeRequired)
                {
                    var InvokeCallback = new Action<TextBox, string,int, bool>(AppendTextAsync);
                    if (textbox.IsDisposed == true)
                        return;
                    textbox.Invoke(InvokeCallback, textbox, txt, lines, isTime);
                }
                else
                {
                    if (textbox.IsDisposed)
                        return;
                    if (textbox.Lines.Length >= lines)
                    {
                        textbox.Clear();
                        //count = 0;
                    }
                    if (isTime)
                    {
                        textbox.AppendText($"【{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")}】{txt}\r\n");
                        //count++;
                    }
                    else
                    {
                        textbox.AppendText($"行号:{textbox.Lines.Length}:{txt}\r\n");
                        //count++;
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message + ex.StackTrace);
            }
        }
    }
}
